<?php
	class _fo_products
	{
		var $_sum = array();
		var $_last_id = 0;
		var $_sum_id = 0;
		var $_del = false;
		var $_maj = false;
		var $_id = 0;
		var $_db;
		var $_wrapper;
		var $_pk = 'id';
		var $_count = 0;
		var $_res = array();
		var $_fields = array();
		public $id;
		public $datecrea;
		public $datemodif;
		public $id_line;
		public $ordre;
		public $num_image;
		public $link_eshop;
		public $name;
		public $type;
		public $contenance;
		public $description;
		public $fragrances;
		
		static $_instance;
		
		// singleton
		public function &getInstance() 
		{
			if (!self::$_instance instanceof self)
				self::$_instance = new self();
			return self::$_instance;
		}
		
		function __construct()
		{
			$this -> _wrapper = GP_wrapper::getInstance();
			$this -> _wrapper -> settable('_fo_products');
			$this -> _db = GP_db::getInstance(BDD_DSN, BDD_UTILISATEUR, BDD_MOT_DE_PASSE);
			$this -> _pk = $this -> _wrapper -> _pk;
			$this -> fields();
		}
		
		function fields()
		{
			$q = "DESCRIBE _fo_products";
			$res = $this -> _db -> fetchArray($q);
			foreach ($res as $f => $tab)
			{
				foreach ($tab as $type => $champ)
				{
					if ($type == 'Field')
					{
						$this -> $champ = '';
						$this -> _fields[] = $champ;
					}
				}
			}
		}
		
		function setId($val)
		{
			$this -> id = $val;
		}
		
		function getId()
		{
			return $this -> id;
		}

		function setDatecrea($val)
		{
			$this -> datecrea = $val;
		}
		
		function getDatecrea()
		{
			return $this -> datecrea;
		}

		function setDatemodif($val)
		{
			$this -> datemodif = $val;
		}
		
		function getDatemodif()
		{
			return $this -> datemodif;
		}

		function setIdline($val)
		{
			$this -> id_line = $val;
		}
		
		function getIdline()
		{
			return $this -> id_line;
		}

		function setOrdre($val)
		{
			$this -> ordre = $val;
		}
		
		function getOrdre()
		{
			return $this -> ordre;
		}

		function setNumimage($val)
		{
			$this -> num_image = $val;
		}
		
		function getNumimage()
		{
			return $this -> num_image;
		}

		function setLinkeshop($val)
		{
			$this -> link_eshop = $val;
		}
		
		function getLinkeshop()
		{
			return $this -> link_eshop;
		}

		function setName($val)
		{
			$this -> name = $val;
		}
		
		function getName()
		{
			return $this -> name;
		}

		function setType($val)
		{
			$this -> type = $val;
		}
		
		function getType()
		{
			return $this -> type;
		}

		function setContenance($val)
		{
			$this -> contenance = $val;
		}
		
		function getContenance()
		{
			return $this -> contenance;
		}

		function setDescription($val)
		{
			$this -> description = $val;
		}
		
		function getDescription()
		{
			return $this -> description;
		}

		function setFragrances($val)
		{
			$this -> fragrances = $val;
		}
		
		function getFragrances()
		{
			return $this -> fragrances;
		}
		
		function insert($ins)
		{
			$this -> _wrapper -> insert($ins);
			return $this -> LastId();
		}
		
		function update($maj, $condition = '')
		{
			$res = $this -> _wrapper -> update($maj, $condition);
			$this -> _maj = $res;
		}
		
		function updateid($maj, $id)
		{
			$res = $this -> _wrapper -> updateid($maj, $id);
			$this -> _id = $id;
			$this -> _maj = $res;
		}
		
		function delete($condition = '')
		{
			$res = $this -> _wrapper -> delete($condition);
			$this -> _del = $res;
		}
		
		function deleteid($id)
		{
			$res = $this -> _wrapper -> deleteid($id);
			$this -> _id = $id;
			$this -> _del = $res;
		}
		
		function count($condition = '')
		{
			$this -> _count = $this -> _wrapper -> count($condition);
		}
		
		function select($champs = '', $condition = '')
		{
			if ((strlen($champs) || count($champs)) && strlen($condition))
				$res = $this -> _wrapper -> select($champs, $condition);
			else if ((!strlen($champs) && !count($champs)) && strlen($condition))
				$res = $this -> _wrapper -> select('', $condition);
			else if ((strlen($champs) || count($champs)) && !strlen($condition))
				$res = $this -> _wrapper -> select($champs);
			$this -> _count = count($res);
			$this -> _res = $res;
		}
		
		function selectid($id, $champs = '')
		{
			if (strlen($champs) || count($champs))
				$res = $this -> _wrapper -> selectid($id, $champs);
			else
				$res = $this -> _wrapper -> selectid($id);
			
			foreach ($res as $tab)
				foreach($tab as $champ => $valeur)
					$this -> $champ = $valeur;
			$this -> _id = $id;
		}
		
		function sum($champs, $condition = '')
		{
			$res = $this -> _wrapper -> sum($champs, $condition);
			if (is_array($res))
			{
				foreach ($res as $champ => $valeur)
					$this -> $champ = $valeur;
			}
			else
				$this -> _sum = $res;
		}
		
		function sumid($champs, $id)
		{
			$this -> _id = $id;
			$res = $this -> _wrapper -> sumid($champs, $id);
			$this -> _sum_id = $res;
		}
		
		function LastId()
		{
			$this -> _id = $this -> _last_id = $this -> _wrapper -> LastId();
			return $this -> _last_id;
		}
		
		function add()
		{
			GP_ActiveRecord::setDefaultDBConnection($this -> _db);
			$ins = new GP_ActiveRecord('_fo_products');
			
			foreach ($this -> _fields as $champ)
				$ins -> $champ = $this -> $champ;
			$ins -> soumettre();
			$this -> _id = $ins -> getKey();
		}
		
		function maj()
		{
			GP_ActiveRecord::setDefaultDBConnection($this -> _db);
			$maj = new GP_ActiveRecord('_fo_products', $this -> _id);
			$tabold = $maj -> get($this -> _fields, $this -> _pk . " = '" . $this -> _id . "'");
			$old = $tabold[0];
			
			foreach ($this -> _fields as $champ)
				$maj -> $champ = (strlen($this -> $champ)) ? $this -> $champ : $old[$champ];
			$maj -> soumettre();
		}
		
		function save()
		{
			if (isset($this -> _id))
				$this -> maj();
			else
				$this -> add();
		}
		
		function get()
		{
			if (strlen($this -> _id) > 0)
			{
				GP_ActiveRecord::setDefaultDBConnection($this -> _db);
				$get = new GP_ActiveRecord('_fo_products', $this -> _id);
				$tab = $get -> get($this -> _fields, $this -> _pk . " = '" . $this -> _id . "'");
				$data = $tab[0];
				foreach ($this -> _fields as $champ)
					$this -> $champ = $data[$champ];
			}
		}
	}
?>